package com.jdk.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @Description :
 * @FileName: ScheduledExecutor.java
 * @Author :WeiHui.Zhang
 * @Data : 2016年2月15日 下午5:19:24
 * @Version:V1.00
 */
public class ScheduledExecutor {

	static class SegTF implements ThreadFactory {

		public Thread newThread(Runnable r) {
			Thread t = new Thread(r, "SegmentScheduledExecutorThread");
			t.setDaemon(true);
			return t;
		}

	}

	public final static ScheduledExecutorService ScheduledService = Executors
			.newSingleThreadScheduledExecutor(new SegTF());

	public static void submit(Runnable cmd, long periodMilliSenconds) {
		ScheduledService.scheduleAtFixedRate(cmd, 10l, periodMilliSenconds, TimeUnit.MILLISECONDS);
	}

	public static void main(String[] args) throws InterruptedException {
		ScheduledExecutor.submit(new Runnable() {

			public void run() {
				System.out.println("do something");
			}

		}, 1000);
		ScheduledExecutor.submit(new Runnable() {

			public void run() {
				System.out.println("do another thing");

			}

		}, 1000);
		TimeUnit.SECONDS.sleep(10);
	}

}
